package com.ultrapower.bo;

import java.util.Arrays;

import com.ultrapower.util.AgentEventType;

public class AgentEventBo {
	public static final int CURRENT_VERSION = 0;

	private final byte version;
	private final String agentId;
	private final long startTimestamp;
	private final long eventTimestamp;
	private final AgentEventType eventType;
	private byte[] eventBody;

	public AgentEventBo(String agentId, long startTimestamp, long eventTimestamp, AgentEventType eventType) {
		this(CURRENT_VERSION, agentId, startTimestamp, eventTimestamp, eventType);
	}

	public AgentEventBo(int version, String agentId, long startTimestamp, long eventTimestamp,
			AgentEventType eventType) {
		if (version < 0 || version > 255) {
			throw new IllegalArgumentException("version out of range (0~255)");
		}
		if (agentId == null) {
			throw new IllegalArgumentException("agentId cannot be null");
		}
		if (agentId.isEmpty()) {
			throw new IllegalArgumentException("agentId cannot be empty");
		}
		if (startTimestamp < 0) {
			throw new IllegalArgumentException("startTimestamp cannot be less than 0");
		}
		if (eventTimestamp < 0) {
			throw new IllegalArgumentException("eventTimestamp cannot be less than 0");
		}
		if (eventType == null) {
			throw new IllegalArgumentException("agentEventType cannot be null");
		}
		this.version = (byte) (version & 0xFF);
		this.agentId = agentId;
		this.startTimestamp = startTimestamp;
		this.eventTimestamp = eventTimestamp;
		this.eventType = eventType;
	}

	public int getVersion() {
		return this.version & 0xFF;
	}

	public String getAgentId() {
		return agentId;
	}

	public long getStartTimestamp() {
		return startTimestamp;
	}

	public long getEventTimestamp() {
		return eventTimestamp;
	}

	public AgentEventType getEventType() {
		return eventType;
	}

	public byte[] getEventBody() {
		return eventBody;
	}

	public void setEventBody(byte[] eventBody) {
		this.eventBody = eventBody;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((agentId == null) ? 0 : agentId.hashCode());
		result = prime * result + (int) (eventTimestamp ^ (eventTimestamp >>> 32));
		result = prime * result + ((eventType == null) ? 0 : eventType.hashCode());
		result = prime * result + (int) (startTimestamp ^ (startTimestamp >>> 32));
		result = prime * result + version;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		AgentEventBo other = (AgentEventBo) obj;
		if (agentId == null) {
			if (other.agentId != null)
				return false;
		} else if (!agentId.equals(other.agentId))
			return false;
		if (eventTimestamp != other.eventTimestamp)
			return false;
		if (eventType != other.eventType)
			return false;
		if (startTimestamp != other.startTimestamp)
			return false;
		if (version != other.version)
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "AgentEventBo [version=" + version + ", agentId=" + agentId + ", startTimestamp=" + startTimestamp
				+ ", eventTimestamp=" + eventTimestamp + ", eventType=" + eventType + ", eventBody="
				+ Arrays.toString(eventBody) + "]";
	}

}
